package com.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.IdWorker;
import com.example.common.R;
import com.example.dao.SetmealDishDao;
import com.example.domain.Category;
import com.example.domain.Setmeal;
import com.example.domain.SetmealDish;
import com.example.dto.SetmealDto;
import com.example.service.CategoryServicce;
import com.example.service.SetmealDishService;
import com.example.service.SetmealService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@ResponseBody
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryServicce categoryServicce;
    @Autowired
    private SetmealDishService setmealDishService;
    @GetMapping("/page")
    public R<Page> getSetmealPage(int page,int pageSize,String name)
    {
        int pageCount=(page-1)*pageSize;
        Page<SetmealDto> setmealDtoPage=new Page<>(page,pageSize);
        if(name==null)
        {
            Page<Setmeal> p=new Page<>(page,pageSize);
            List<Setmeal> selectall = setmealService.selectall(pageCount, pageSize);
            p.setRecords(selectall);
            int selectcount = setmealService.selectcount();
            p.setTotal(selectcount);

            BeanUtils.copyProperties(p,setmealDtoPage,"records");
            List<SetmealDto> lists=new ArrayList<>();
            for(int i=0;i<selectall.size();i++)
            {
                Setmeal setmeal = selectall.get(i);
                SetmealDto setmealDto=new SetmealDto();
                BeanUtils.copyProperties(setmeal,setmealDto);
                Long categoryId = setmeal.getCategoryId();
                Category category = categoryServicce.selectbycategoryId(categoryId);
                String name1 = category.getName();
                setmealDto.setCategoryName(name1);
                lists.add(setmealDto);
            }
            setmealDtoPage.setRecords(lists);
            return R.success(setmealDtoPage);
        }
        else
        {
            Page<Setmeal> p=new Page<>(page,pageSize);
            List<Setmeal> selectbyname = setmealService.selectbyname(pageCount, pageSize, name);
            p.setRecords(selectbyname);
            int selectcount = setmealService.selectcount();
            p.setTotal(selectcount);

            BeanUtils.copyProperties(p,setmealDtoPage,"records");
            List<SetmealDto> lists=new ArrayList<>();
            for(int i=0;i<selectbyname.size();i++)
            {
                Setmeal setmeal = selectbyname.get(i);
                SetmealDto setmealDto=new SetmealDto();
                BeanUtils.copyProperties(setmeal,setmealDto);
                Long categoryId = setmeal.getCategoryId();
                Category category = categoryServicce.selectbycategoryId(categoryId);
                String name1 = category.getName();
                setmealDto.setCategoryName(name1);
                lists.add(setmealDto);
            }
            setmealDtoPage.setRecords(lists);
            return R.success(setmealDtoPage);
        }
    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody SetmealDto setmealDto)
    {
        Long getId = (Long) request.getSession().getAttribute("employee");
        IdWorker idWorker=new IdWorker();
        long id = idWorker.nextId();
        setmealDto.setId(id);
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setCreateUser(getId);
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setUpdateUser(getId);
        setmealDto.setIsDeleted(0);
        setmealService.savesetmeal(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for(int i=0;i<setmealDishes.size();i++)
        {
            SetmealDish setmealDish = setmealDishes.get(i);
            setmealDish.setId(idWorker.nextId());
            setmealDish.setSetmealId(id);
            setmealDish.setSort(0);
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setCreateUser(getId);
            setmealDish.setUpdateUser(getId);
            setmealDish.setIsDeleted(0);
            setmealDishService.savesetmealdish(setmealDish);
        }
        return R.success("保存成功");
    }

    @DeleteMapping
    public R<String> delete(String ids)
    {
        String[] split = ids.split(",");
        List<Long> id=new ArrayList<>();
        for(int i=0;i<split.length;i++)
        {
            id.add(Long.valueOf(split[i]));
        }
        for(int i=0;i<id.size();i++)
        {
            int status = setmealService.getstatusbyid(id.get(i));
            if(status==1)
            {
                String getnamebyid = setmealService.getnamebyid(id.get(i));
                return R.error("请将套餐"+getnamebyid+"停售后再删除");
            }
        }
        for(int i=0;i<id.size();i++)
        {
            setmealService.deletebyid(id.get(i));
            setmealDishService.deletebysetmealid(id.get(i));
        }
        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public R<String> setstatus(HttpServletRequest request,@PathVariable int status,String ids)
    {
        Long getId = (Long) request.getSession().getAttribute("employee");
        String[] split = ids.split(",");
        List<Long> id=new ArrayList<>();
        for(int i=0;i<split.length;i++)
        {
            id.add(Long.valueOf(split[i]));
        }
        for(int i=0;i<id.size();i++)
        {
            Setmeal setmeal=new Setmeal();
            setmeal.setId(id.get(i));
            setmeal.setStatus(status);
            setmeal.setUpdateTime(LocalDateTime.now());
            setmeal.setUpdateUser(getId);
            setmealService.updatestatusbyid(setmeal);
        }
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Long categoryId,Integer status)
    {
        Setmeal setmeal=new Setmeal();
        setmeal.setCategoryId(categoryId);
        setmeal.setStatus(status);
        List<Setmeal> selectbycategoryandstatus = setmealService.selectbycategoryandstatus(setmeal);
        return R.success(selectbycategoryandstatus);
    }
}
